package com.bluesmart.mia;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLayoutChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NewNoteActivity extends BaseActivity implements OnClickListener{
	int mSelected = 0;
	String[] mPicker0;
	String[] mPicker1;
	String[] mPicker2;
	String[] mPicker3;
	int mNoteType = 0;
	int mSepIndex = 0;
	boolean keyboardShow = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_note);

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

		findViewById(R.id.img0).setOnClickListener(this);
		findViewById(R.id.img1).setOnClickListener(this);
		findViewById(R.id.img2).setOnClickListener(this);
		findViewById(R.id.img3).setOnClickListener(this);
		findViewById(R.id.img4).setOnClickListener(this);
		findViewById(R.id.img5).setOnClickListener(this);
		findViewById(R.id.img6).setOnClickListener(this);
		findViewById(R.id.img7).setOnClickListener(this);
		findViewById(R.id.img8).setOnClickListener(this);
		
		findViewById(R.id.btn0).setOnClickListener(this);
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
		findViewById(R.id.btn3).setOnClickListener(this);
		findViewById(R.id.btn4).setOnClickListener(this);
		findViewById(R.id.btn5).setOnClickListener(this);
		findViewById(R.id.date).setOnClickListener(this);
		findViewById(R.id.change).setOnClickListener(this);
		
		((TextView)findViewById(R.id.date)).setText(Utils.ConverToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
		initDate();
		
		listenKeyboard();
	}

	void listenKeyboard()
	{
		final View activityRootView = findViewById(R.id.screen);  
		final int screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();  
        final int keyHeight = screenHeight/3;
		
        activityRootView.addOnLayoutChangeListener(new OnLayoutChangeListener(){

			@Override
			public void onLayoutChange(View v, int left, int top, int right,  
		            int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
				if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){  

					((ImageView)findViewById(R.id.btn0)).setBackgroundResource(R.drawable.corner_note);
					((ImageView)findViewById(R.id.btn1)).setBackgroundResource(R.drawable.corner_note);
					((ImageView)findViewById(R.id.btn2)).setBackgroundResource(R.drawable.corner_note);
					((ImageView)findViewById(R.id.btn3)).setBackgroundResource(R.drawable.corner_note);
					((ImageView)findViewById(R.id.btn4)).setBackgroundResource(R.drawable.corner_note);
					((ImageView)findViewById(R.id.btn5)).setBackgroundResource(R.drawable.corner_note);
					
					((TextView)findViewById(R.id.date)).setTextColor(Color.parseColor("#AAAAAA"));
					((TextView)findViewById(R.id.date)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.notetime, 0, 0, 0);
					
					if(findViewById(R.id.bottom_layout).getVisibility() == View.VISIBLE)
					{
						findViewById(R.id.bottom_layout).setVisibility(View.GONE);
					}
					
					keyboardShow = true;
		        }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){  
		        	keyboardShow = false;
		        }  
			}
		});
	}
	
	@Override
	public void onClick(View arg0) {
		findViewById(R.id.dot).setVisibility(View.GONE);
		if(arg0.getId() == R.id.left) finish();
		else if(arg0.getId() == R.id.right) sendHttpRequest();
		else if(arg0.getId() == R.id.btn1)
		{
			showPickView(1);
		}
		else if(arg0.getId() == R.id.btn2)
		{
			showPickView(2);
		}
		else if(arg0.getId() == R.id.btn3)
		{
			findViewById(R.id.dot).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.dot)).setText(".");
			showPickView(3);
		}
		else if(arg0.getId() == R.id.btn4)
		{
			findViewById(R.id.dot).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.dot)).setText(":");
			showPickView(4);
		}
		else if(arg0.getId() == R.id.btn5)
		{
			showPickView(5);
		}
		else if(arg0.getId() == R.id.change)
		{
			WheelView number = (WheelView)findViewById(R.id.picker0);
			WheelView number0 = (WheelView)findViewById(R.id.picker1);
			WheelView number1 = (WheelView)findViewById(R.id.picker2);
			WheelView number2 = (WheelView)findViewById(R.id.picker3);
			if(mNoteType == 1)
			{
				String oldStr = ((TextView)findViewById(R.id.note_0)).getText().toString();
				String newStr = mPicker0[number.getCurrentItem()];
				if(oldStr.indexOf(newStr) > 0)
				{
					findViewById(R.id.note_0).setVisibility(View.GONE);
					((TextView)findViewById(R.id.note_0)).setText("");
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
				}
				else
				{
					findViewById(R.id.note_0).setVisibility(View.VISIBLE);
					((TextView)findViewById(R.id.note_0)).setText(getString(R.string.note2) + ": " + newStr);
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
				}
			}
			else if(mNoteType == 2)
			{
				String oldStr = ((TextView)findViewById(R.id.note_1)).getText().toString();
				String newStr = mPicker0[number.getCurrentItem()];
				if(oldStr.indexOf(newStr) > 0)
				{
					findViewById(R.id.note_1).setVisibility(View.GONE);
					((TextView)findViewById(R.id.note_1)).setText("");
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
				}
				else
				{
					findViewById(R.id.note_1).setVisibility(View.VISIBLE);
					((TextView)findViewById(R.id.note_1)).setText(getString(R.string.note5) + ": " + newStr);
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
				}
			}
			else if(mNoteType == 3)
			{
				String oldStr = ((TextView)findViewById(R.id.note_2)).getText().toString();
				String newStr = "";
				if(((DemoApp) getApplicationContext()).glbUnit == 1)
				{
					newStr = mPicker0[number.getCurrentItem()] + "." + mPicker1[number0.getCurrentItem()] + "inch-" + 
							mPicker2[number1.getCurrentItem()] + "." + mPicker3[number2.getCurrentItem()] + "lb";
				}
				else
				{
					newStr = mPicker0[number.getCurrentItem()] + "." + mPicker1[number0.getCurrentItem()] + "cm-" + 
							mPicker2[number1.getCurrentItem()] + "." + mPicker3[number2.getCurrentItem()] + "kg";
				}
				
				if(oldStr.indexOf(newStr) > 0)
				{
					findViewById(R.id.note_2).setVisibility(View.GONE);
					((TextView)findViewById(R.id.note_2)).setText("");
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
				}
				else
				{
					findViewById(R.id.note_2).setVisibility(View.VISIBLE);
					((TextView)findViewById(R.id.note_2)).setText(getString(R.string.note3) + ": " + newStr);
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
				}
			}
			else if(mNoteType == 4)
			{
				String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
				String newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number0.getCurrentItem()] + "-" + 
						mPicker2[number1.getCurrentItem()] + ":" + mPicker3[number2.getCurrentItem()];
				if(oldStr.indexOf(newStr) > 0)
				{
					findViewById(R.id.note_1).setVisibility(View.GONE);
					((TextView)findViewById(R.id.note_3)).setText("");
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
				}
				else
				{
					findViewById(R.id.note_3).setVisibility(View.VISIBLE);
					((TextView)findViewById(R.id.note_3)).setText(getString(R.string.note4) + ": " + newStr);
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
				}
			}
			else if(mNoteType == 5)
			{
				String oldStr = ((TextView)findViewById(R.id.note_4)).getText().toString();
				String newStr = mPicker0[number.getCurrentItem()];
				if(oldStr.indexOf(newStr) > 0)
				{
					findViewById(R.id.note_4).setVisibility(View.GONE);
					((TextView)findViewById(R.id.note_4)).setText("");
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
				}
				else
				{
					findViewById(R.id.note_4).setVisibility(View.VISIBLE);
					((TextView)findViewById(R.id.note_4)).setText(getString(R.string.note6) + ": " + newStr);
					((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
				}
			}
			else if(mNoteType == 6)
			{
				WheelView year = (WheelView)findViewById(R.id.picker0);
		        WheelView hour = (WheelView)findViewById(R.id.picker1);
		        WheelView min = (WheelView)findViewById(R.id.picker3);
		        
		        Calendar cal = Calendar.getInstance();
				cal.setTime(new Date(System.currentTimeMillis()));
				
				Date firstDay = Utils.ConverToDate(cal.get(Calendar.YEAR) + "-01-01", "yyyy-MM-dd");
		        cal.setTime(firstDay);
		        cal.add(Calendar.DAY_OF_MONTH, year.getCurrentItem());
		        
		        String text = Utils.ConverToString(cal.getTime(), "yyyy-MM-dd");
		        text += " " + hourStr[hour.getCurrentItem()] + ":" + minStr[min.getCurrentItem()] + ":00";
		        text = text.replace("时", "");
		        text = text.replace("分", "");
		        
				((TextView)findViewById(R.id.date)).setText(text);
			}
		}
		else if(arg0.getId() == R.id.btn0)
		{
			if(mPhotoFile[8] != null)
			{
				ToastUtil.showToastWithAlertPic(R.string.max_pic);
				return;
			}
			showImagePicker();
		}
		else if(arg0.getId() == R.id.date)
		{
			showDate(6);
		}
		else 
		{
			mSelected = arg0.getId() - R.id.img0;
			

			final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
			
			OnClickListener listen = new OnClickListener()
			{
				@Override
				public void onClick(View arg0) {
					if(arg0.getId() == R.id.cancel) mDialog.dismiss();
					else if(arg0.getId() == R.id.pic)
					{
						mDialog.dismiss();
						Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
						getAlbum.setType("image/*");
						startActivityForResult(getAlbum, 3);
					}
					else if(arg0.getId() == R.id.camera)
					{
						mDialog.dismiss();
						startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), 4);
					}
					else
					{
						mDialog.dismiss();
						
						for(int i = mSelected; i < 8; i++)
						{
							mPhotoFile[i] = mPhotoFile[i + 1];
							if(mPhotoFile[i] != null)
							{
								((ImageView)findViewById(R.id.img0 + i)).setImageBitmap(mBitmap[i + 1]);
							}
							else
							{
								findViewById(R.id.img0 + i).setVisibility(View.GONE);
							}
						}
						
						mPhotoFile[8] = null;
						mBitmap[8] = null;
						
						findViewById(R.id.img0 + 8).setVisibility(View.GONE);
					}
				}
			};

			mDialog.setContentView(R.layout.dialog_img_choose);
			mDialog.findViewById(R.id.camera).setVisibility(View.GONE);
			mDialog.findViewById(R.id.pic).setVisibility(View.GONE);
			mDialog.findViewById(R.id.cancel).setOnClickListener(listen);
			mDialog.findViewById(R.id.del).setOnClickListener(listen);
			mDialog.findViewById(R.id.del).setVisibility(View.VISIBLE);
			mDialog.show();
		}
	}
	
	String[] yearStr;
	String[] hourStr = new String[24];
	String[] minStr = new String[60];
	void initDate()
	{
		new Thread()
		{
			public void run()
			{
				Calendar cal = Calendar.getInstance();
				cal.setTime(new Date(System.currentTimeMillis()));
				int now = cal.get(Calendar.DAY_OF_YEAR);
				
				Date firstDay = Utils.ConverToDate(cal.get(Calendar.YEAR) + "-01-01", "yyyy-MM-dd");
		        cal.setTime(firstDay);
		        
		        int day = now - cal.get(Calendar.DAY_OF_YEAR);
		        yearStr = new String[day + 1];
		        
				for(int i = 0; i <= day || i < 60; i++)
				{
					if(i < 24) hourStr[i] = String.valueOf(i<10?("0" + i):i) + getString(R.string.hour);
					if(i < 60) minStr[i] = String.valueOf(i<10?("0" + i):i) + getString(R.string.min);

					if(i <= day)
					{
						yearStr[i] = Utils.ConverToString(cal.getTime(), "MM-dd EE");
				        cal.add(Calendar.DAY_OF_MONTH, 1);
					}
				}
			}
		}.start();
	}
	
	void showDate(int index)
	{
		((ImageView)findViewById(R.id.btn0)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn1)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn2)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn3)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn4)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn5)).setBackgroundResource(R.drawable.corner_note);

		((TextView)findViewById(R.id.date)).setTextColor(Color.parseColor("#AAAAAA"));
		((TextView)findViewById(R.id.date)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.notetime, 0, 0, 0);
		
		if(findViewById(R.id.bottom_layout).getVisibility() == View.VISIBLE && index == mNoteType)
		{
			findViewById(R.id.bottom_layout).setVisibility(View.GONE);
			return;
		}

		((TextView)findViewById(R.id.date)).setTextColor(Color.parseColor("#e19a47"));
		((TextView)findViewById(R.id.date)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.notetimeselect, 0, 0, 0);
		
		mNoteType = index;
		
		findViewById(R.id.text0).setVisibility(View.GONE);
		findViewById(R.id.text1).setVisibility(View.GONE);
		findViewById(R.id.text2).setVisibility(View.GONE);
		findViewById(R.id.timelabel).setVisibility(View.GONE);
		
		findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
		Date date = new Date(System.currentTimeMillis());
		
		WheelView year = (WheelView)findViewById(R.id.picker0);
        year.setViewAdapter(new ArrayWheelAdapter<String>(this, yearStr,Color.parseColor("#FFFFFF")));
        year.setCurrentItem(yearStr.length - 1);
		
        WheelView hour = (WheelView)findViewById(R.id.picker1);
        hour.setViewAdapter(new ArrayWheelAdapter<String>(this, hourStr,Color.parseColor("#FFFFFF")));
        hour.setCurrentItem(date.getHours());
        
        WheelView min = (WheelView)findViewById(R.id.picker3);
        min.setViewAdapter(new ArrayWheelAdapter<String>(this, minStr,Color.parseColor("#FFFFFF")));
        min.setCurrentItem(date.getMinutes());
	
        year.setVisibility(View.VISIBLE);
        hour.setVisibility(View.VISIBLE);
        min.setVisibility(View.VISIBLE);
        
        findViewById(R.id.picker2).setVisibility(View.GONE);
	}
	
	void showPickView(int index)
	{
		((ImageView)findViewById(R.id.btn0)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn1)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn2)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn3)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn4)).setBackgroundResource(R.drawable.corner_note);
		((ImageView)findViewById(R.id.btn5)).setBackgroundResource(R.drawable.corner_note);
		
		((TextView)findViewById(R.id.date)).setTextColor(Color.parseColor("#AAAAAA"));
		((TextView)findViewById(R.id.date)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.notetime, 0, 0, 0);
		
		if(findViewById(R.id.bottom_layout).getVisibility() == View.VISIBLE && index == mNoteType)
		{
			findViewById(R.id.bottom_layout).setVisibility(View.GONE);
			return;
		}

		if(keyboardShow)
		{
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
			imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
		}
		
		mNoteType = index;
		
		findViewById(R.id.text0).setVisibility(View.GONE);
		findViewById(R.id.text1).setVisibility(View.GONE);
		findViewById(R.id.text2).setVisibility(View.GONE);
		findViewById(R.id.timelabel).setVisibility(View.GONE);
		
		findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
		if(mNoteType == 1)
		{
			mPicker0 = new String[4];
			mPicker0[0] = getString(R.string.quick_0);
			mPicker0[1] = getString(R.string.quick_1);
			mPicker0[2] = getString(R.string.quick_2);
			mPicker0[3] = getString(R.string.quick_3);
			
			((ImageView)findViewById(R.id.btn1)).setBackgroundResource(R.drawable.corner_note_s);
			findViewById(R.id.text2).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.text2)).setText(getString(R.string.note2));
		}
		else if(mNoteType == 2)
		{
			mPicker0 = new String[7];
			mPicker0[0] = "蛋黄糊";
			mPicker0[1] = "水果泥";
			mPicker0[2] = "米糊";
			mPicker0[3] = "粥";
			mPicker0[4] = "米汤";
			mPicker0[5] = "果汁";
			mPicker0[6] = "蔬菜汁";
			
			((ImageView)findViewById(R.id.btn2)).setBackgroundResource(R.drawable.corner_note_s);
			findViewById(R.id.text2).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.text2)).setText(getString(R.string.note5));
		}
		else if(mNoteType == 3)
		{
			
			mPicker1 = new String[10];
			mPicker2 = new String[12];
			mPicker3 = new String[10];
			
			int countW = 30;
			int count = 101;
			if(((DemoApp) getApplicationContext()).glbUnit == 1) 
			{
				count = 41;
				countW = 57;
				((TextView)findViewById(R.id.unit)).setText("inch");
			}
			mPicker0 = new String[count];
			mPicker2 = new String[countW];
			for(int i = 0; i < count; i++)
			{
				if(((DemoApp) getApplicationContext()).glbUnit == 1) mPicker0[i] = String.valueOf(i + 12);
				else mPicker0[i] = String.valueOf(i + 30);
				
				if(i < 10)
				{
					mPicker1[i] = "" + i;
					mPicker3[i] = "" + i;
				}
				
				if(countW > i)
				{
					if(((DemoApp) getApplicationContext()).glbUnit == 1) mPicker2[i] = String.valueOf(i + 8);
					else mPicker2[i] = String.valueOf(i + 1);
				}
			}
			
			if(((DemoApp) getApplicationContext()).glbUnit == 1)
			{
				((TextView)findViewById(R.id.text0)).setText(getString(R.string.height) + "(inch)");
				((TextView)findViewById(R.id.text1)).setText(getString(R.string.weight)  + "(lb)");
			}
			else
			{
				((TextView)findViewById(R.id.text0)).setText(getString(R.string.height) + "(cm)");
				((TextView)findViewById(R.id.text1)).setText(getString(R.string.weight)  + "(kg)");
			}
			
			((ImageView)findViewById(R.id.btn3)).setBackgroundResource(R.drawable.corner_note_s);
			findViewById(R.id.text0).setVisibility(View.VISIBLE);
			findViewById(R.id.text1).setVisibility(View.VISIBLE);
		}
		else if(mNoteType == 4)
		{
			mPicker0 = new String[24];
			mPicker1 = new String[60];
			mPicker2 = new String[17];
			mPicker3 = new String[60];
			
			int start = new Date().getHours() - 23;
			for(int i = 0; i < 60; i++)
			{
				if(i < 24)
				{
					if(start < 0) start += 24;
					
					if(start < 10) mPicker0[i] = "0" + start;
					else mPicker0[i] = "" + start;
					
					if(start == 0) mSepIndex = i;
					if(start == 23) start = 0;
					else start++;
				}
				
				if(i < 7)
				{
					mPicker1[i] = "0" + i;
					mPicker2[i] = "0" + i;
					mPicker3[i] = "0" + i;
				}
				else if(i < 10)
				{
					mPicker1[i] = "0" + i;
					mPicker2[i] = "0" + i;
					mPicker3[i] = "0" + i;
				}
				else if(i < 17)
				{
					mPicker1[i] = "" + i;
					mPicker2[i] = "" + i;
					mPicker3[i] = "" + i;
				}
				else if(i < 24)
				{
					mPicker1[i] = "" + i;
					mPicker3[i] = "" + i;
				}
				else
				{
					mPicker1[i] = "" + i;
					mPicker3[i] = "" + i;
				}
			}
			
			((ImageView)findViewById(R.id.btn4)).setBackgroundResource(R.drawable.corner_note_s);
			findViewById(R.id.text0).setVisibility(View.VISIBLE);
			findViewById(R.id.text1).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.text0)).setText(getString(R.string.start_time));
			((TextView)findViewById(R.id.text1)).setText(getString(R.string.end_time));
			findViewById(R.id.timelabel).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.timelabel)).setText(getString(R.string.yestoday));
		}
		else if(mNoteType == 5)
		{
			String[] goal = {"哭","笑","爬","抬头","翻身","尿尿","咿哑语","叫妈妈","叫爸爸","坐","站","走","打针","受伤","吃药","长牙牙",
					"量体温","吃手指","会跳","会跑","摔跤","刷牙","游泳","坐车","滑滑梯","坐飞机","数数字","唱歌","画画","上课","公园","生日","理发"};
			
			mPicker0 = new String[goal.length];
			for(int i = 0; i < goal.length; i++)
			{
				mPicker0[i] = goal[i];
			}
			
			((ImageView)findViewById(R.id.btn5)).setBackgroundResource(R.drawable.corner_note_s);
			findViewById(R.id.text2).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.text2)).setText(getString(R.string.note6));
		}

		final WheelView number = (WheelView)findViewById(R.id.picker0);
    	final WheelView number1 = (WheelView)findViewById(R.id.picker1);
    	final WheelView number2 = (WheelView)findViewById(R.id.picker2);
    	final WheelView number3 = (WheelView)findViewById(R.id.picker3);
    	

    	number.setCurrentItem(0);
    	number1.setCurrentItem(0);
    	number2.setCurrentItem(0);
    	number3.setCurrentItem(0);
    	
    	number1.setVisibility(View.GONE);
    	number2.setVisibility(View.GONE);
    	number3.setVisibility(View.GONE);
    	
        number.setViewAdapter(new ArrayWheelAdapter<String>(this, mPicker0,Color.parseColor("#FFFFFF")));
        number.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if(mNoteType == 1)
				{
					String oldStr = ((TextView)findViewById(R.id.note_0)).getText().toString();
					String newStr = mPicker0[newValue];
					if(oldStr.indexOf(newStr) > 0)
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
					}
					else
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
					}
				}
				else if(mNoteType == 2)
				{
					String oldStr = ((TextView)findViewById(R.id.note_1)).getText().toString();
					String newStr = mPicker0[newValue];
					if(oldStr.indexOf(newStr) > 0)
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
					}
					else
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
					}
				}
				else if(mNoteType == 3)
				{
					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
					String newStr = "";
					if(((DemoApp) getApplicationContext()).glbUnit == 1)
					{
						newStr = mPicker0[number.getCurrentItem()] + "." + mPicker1[number1.getCurrentItem()] + "inch-" + 
								mPicker2[number2.getCurrentItem()] + "." + mPicker3[number3.getCurrentItem()] + "lb";
					}
					else
					{
						newStr = mPicker0[number.getCurrentItem()] + "." + mPicker1[number1.getCurrentItem()] + "cm-" + 
								mPicker2[number2.getCurrentItem()] + "." + mPicker3[number3.getCurrentItem()] + "kg";
					}
					
					if(oldStr.indexOf(newStr) > 0)
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
					}
					else
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
					}
				}
				else if(mNoteType == 4)
				{
					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
					String newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "-" + 
							mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()];
					if(oldStr.indexOf(newStr) > 0)
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
					}
					else
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
					}
					
					if(newValue < mSepIndex) ((TextView)findViewById(R.id.timelabel)).setText(getString(R.string.yestoday));
					else ((TextView)findViewById(R.id.timelabel)).setText(getString(R.string.today));
				}
				else if(mNoteType == 5)
				{
					String oldStr = ((TextView)findViewById(R.id.note_4)).getText().toString();
					String newStr = mPicker0[newValue];
					if(oldStr.indexOf(newStr) > 0)
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
					}
					else
					{
						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
					}
				}
			}
        });
        
        if(mNoteType == 3 || mNoteType == 4)
        {
        	number1.setVisibility(View.VISIBLE);
        	number2.setVisibility(View.VISIBLE);
        	number3.setVisibility(View.VISIBLE);
        	
            number1.setViewAdapter(new ArrayWheelAdapter<String>(this, mPicker1,Color.parseColor("#FFFFFF")));
            number1.addChangingListener(new OnWheelChangedListener(){
    			@Override
    			public void onChanged(WheelView wheel, int oldValue, int newValue) {
    				if(mNoteType == 4)
    				{
    					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
    					String newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "-" + 
    							mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()];
    					if(oldStr.indexOf(newStr) > 0)
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
    					}
    					else
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
    					}
    				}
    				else if(mNoteType == 3)
    				{
    					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
    					String newStr = "";
    					if(((DemoApp) getApplicationContext()).glbUnit == 1)
    					{
    						newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "inch-" + 
    								mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()] + "lb";
    					}
    					else
    					{
    						newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "cm-" + 
    								mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()] + "kg";
    					}
    					
    					if(oldStr.indexOf(newStr) > 0)
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
    					}
    					else
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
    					}
    				}
    			}
            });
            
            number2.setViewAdapter(new ArrayWheelAdapter<String>(this, mPicker2,Color.parseColor("#FFFFFF")));
            number2.addChangingListener(new OnWheelChangedListener(){
    			@Override
    			public void onChanged(WheelView wheel, int oldValue, int newValue) {
    				if(mNoteType == 4)
    				{
    					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
    					String newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "-" + 
    							mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()];
    					if(oldStr.indexOf(newStr) > 0)
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
    					}
    					else
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
    					}
    				}
    				else if(mNoteType == 3)
    				{
    					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
    					String newStr = "";
    					if(((DemoApp) getApplicationContext()).glbUnit == 1)
    					{
    						newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "inch-" + 
    								mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()] + "lb";
    					}
    					else
    					{
    						newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "cm-" + 
    								mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()] + "kg";
    					}
    					
    					if(oldStr.indexOf(newStr) > 0)
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
    					}
    					else
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
    					}
    				}
    			}
            });
            
            number3.setViewAdapter(new ArrayWheelAdapter<String>(this, mPicker3,Color.parseColor("#FFFFFF")));
            number3.addChangingListener(new OnWheelChangedListener(){
    			@Override
    			public void onChanged(WheelView wheel, int oldValue, int newValue) {
    				if(mNoteType == 4)
    				{
    					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
    					String newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "-" + 
    							mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()];
    					if(oldStr.indexOf(newStr) > 0)
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
    					}
    					else
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
    					}
    				}
    				else if(mNoteType == 3)
    				{
    					String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
    					String newStr = "";
    					if(((DemoApp) getApplicationContext()).glbUnit == 1)
    					{
    						newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "inch-" + 
    								mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()] + "lb";
    					}
    					else
    					{
    						newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "cm-" + 
    								mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()] + "kg";
    					}
    					
    					if(oldStr.indexOf(newStr) > 0)
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
    					}
    					else
    					{
    						((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
    					}
    				}
    			}
            });
        }
        
        if(mNoteType == 1)
		{
			String oldStr = ((TextView)findViewById(R.id.note_0)).getText().toString();
			String newStr = mPicker0[number.getCurrentItem()];
			if(oldStr.indexOf(newStr) > 0)
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
			}
			else
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
			}
		}
		else if(mNoteType == 2)
		{
			String oldStr = ((TextView)findViewById(R.id.note_1)).getText().toString();
			String newStr = mPicker0[number.getCurrentItem()];
			if(oldStr.indexOf(newStr) > 0)
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
			}
			else
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
			}
		}
		else if(mNoteType == 3)
		{
			String oldStr = ((TextView)findViewById(R.id.note_2)).getText().toString();
			String newStr = "";
			if(((DemoApp) getApplicationContext()).glbUnit == 1)
			{
				newStr = mPicker0[number.getCurrentItem()] + "." + mPicker1[number1.getCurrentItem()] + "inch-" + 
						mPicker2[number2.getCurrentItem()] + "." + mPicker3[number3.getCurrentItem()] + "lb";
			}
			else
			{
				newStr = mPicker0[number.getCurrentItem()] + "." + mPicker1[number1.getCurrentItem()] + "cm-" + 
						mPicker2[number2.getCurrentItem()] + "." + mPicker3[number3.getCurrentItem()] + "kg";
			}
			
			if(oldStr.indexOf(newStr) > 0)
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
			}
			else
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
			}
		}
		else if(mNoteType == 4)
		{
			String oldStr = ((TextView)findViewById(R.id.note_3)).getText().toString();
			String newStr = mPicker0[number.getCurrentItem()] + ":" + mPicker1[number1.getCurrentItem()] + "-" + 
					mPicker2[number2.getCurrentItem()] + ":" + mPicker3[number3.getCurrentItem()];
			if(oldStr.indexOf(newStr) > 0)
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
			}
			else
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
			}
			
			if(mNoteType == 4)
			{
				number.setCurrentItem(mSepIndex);
			}
		}
		else if(mNoteType == 5)
		{
			String oldStr = ((TextView)findViewById(R.id.note_4)).getText().toString();
			String newStr = mPicker0[number.getCurrentItem()];
			if(oldStr.indexOf(newStr) > 0)
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.deletenote);
			}
			else
			{
				((ImageButton)findViewById(R.id.change)).setImageResource(R.drawable.sure);
			}
		}
	}
	
	private void sendHttpRequest() {
		String text = ((EditText)findViewById(R.id.content)).getText().toString();
		if(text.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_tip);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000"))
				{
					if(mPhotoFile[0] == null)
					{
						List<Activity> list = ((DemoApp) getApplicationContext()).getActivityList();
						int count = list.size();
						for(int i = 0; i < count; i++)
						{
							Activity activity = list.get(i);
							if(activity.getClass().getName().contains("NoteViewActivity"))
							{
								((NoteViewActivity)activity).sendChartHttpRequest(true);
								break;
							}
						}
						
						finish();
						ToastUtil.showToastWithAlertPic(R.string.success);
					}
					else
					{
						sendHttpImgRequest(ruser.getString("noteid"));
					}
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", ((DemoApp)getApplicationContext()).getSeletedBaby().getString("babyid"));
//		params.put("notetype", "" + getIntent().getExtras().getInt("type"));
		params.put("handtype", "add");
		params.put("content", text);
		
		String text0 = ((TextView)findViewById(R.id.note_0)).getText().toString();
		String text1 = ((TextView)findViewById(R.id.note_1)).getText().toString();
		String text2 = ((TextView)findViewById(R.id.note_2)).getText().toString();
		String text3 = ((TextView)findViewById(R.id.note_3)).getText().toString();
		String text4 = ((TextView)findViewById(R.id.note_4)).getText().toString();
		
		if(text0.length() > 0) params.put("quicknote", text0.replace(getString(R.string.note2) + ":", ""));
		if(text1.length() > 0) params.put("food", text1.replace(getString(R.string.note5) + ":", ""));
		if(text2.length() > 0)
		{
			text2 = text2.replace(getString(R.string.note3) + ":","");
			text2 = text2.replace("inch", "");
			text2 = text2.replace("lb", "");
			text2 = text2.replace("cm", "");
			text2 = text2.replace("kg", "");
			params.put("height_weight", text2);
		}
		if(text3.length() > 0) params.put("sleep", text3.replace(getString(R.string.note4) + ":", ""));
		if(text4.length() > 0) params.put("milestone", text4.replace(getString(R.string.note6) + ":", ""));
		params.put("addtime", ((TextView)findViewById(R.id.date)).getText().toString());

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/NoteHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	private void sendHttpImgRequest(String noteid) {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					List<Activity> list = ((DemoApp) getApplicationContext()).getActivityList();
					((NoteViewActivity)list.get(list.size() - 2)).sendChartHttpRequest(true);
					
					ToastUtil.showToastWithAlertPic(R.string.success);
					finish();
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else
				{
				}
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("handtype", "note");
		params.put("noteid", noteid);
		try {
			for(int i = 0; i < 9; i++)
			{
				if(mPhotoFile[i] != null)
				{
					params.put("filename" + i, mPhotoFile[i]);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		MgqRestClient.post(Constant.SERVER_URL + "/sw/uploadimg.ashx", params, loginHandler);
	}
	
	void showImagePicker()
	{
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(arg0.getId() == R.id.cancel || R.id.dialog_layout == arg0.getId()) mDialog.dismiss();
				else if(arg0.getId() == R.id.pic)
				{
					mDialog.dismiss();
					Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
					getAlbum.setType("image/*");
					startActivityForResult(getAlbum, 3);
				}
				else if(arg0.getId() == R.id.camera)
				{
					mDialog.dismiss();
					startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), 4);
				}
				else
				{
					mDialog.dismiss();
					
					boolean showAdd = true;
					for(int i = mSelected; i < 8; i++)
					{
						mPhotoFile[i] = mPhotoFile[i + 1];
						if(mPhotoFile[i] != null)
						{
							((ImageView)findViewById(R.id.img0 + i)).setImageBitmap(mBitmap[i + 1]);
						}
						else
						{
							if(!showAdd) 
							{
								findViewById(R.id.img0 + i).setVisibility(View.INVISIBLE);
							}
							showAdd = false;
						}
					}
					
					mPhotoFile[8] = null;
					mBitmap[8] = null;
					
					findViewById(R.id.img0 + 8).setVisibility(View.INVISIBLE);
				}
			}
		};

		mDialog.setContentView(R.layout.dialog_img_choose);
		mDialog.findViewById(R.id.camera).setOnClickListener(listen);
		mDialog.findViewById(R.id.pic).setOnClickListener(listen);
		mDialog.findViewById(R.id.cancel).setOnClickListener(listen);
		mDialog.findViewById(R.id.dialog_layout).setOnClickListener(listen);
		mDialog.findViewById(R.id.del).setOnClickListener(listen);
		mDialog.show();
	}

	private File mPhotoFile[] = new File[9];
	private Bitmap mBitmap[] = new Bitmap[9];
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null)
			return;
		
		Bitmap bitmap = null;
		if (resultCode == Activity.RESULT_OK) {
			if (requestCode == 4) {
				Uri uri = data.getData();
				if (uri != null) {
					bitmap = BitmapFactory.decodeFile(uri.getPath());
				}

				if (bitmap == null) {
					Bundle bundle = data.getExtras();
					if (bundle != null) {
						bitmap = (Bitmap) bundle.get("data");
					}
				}
			} else if (requestCode == 3) {
				Uri uri = data.getData();
				String[] proj = { MediaStore.Images.Media.DATA };
				Cursor cursor = managedQuery(uri, proj, null, null, null);
				int column_index = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToFirst();
				bitmap = Utils
						.getImageThumbnail(cursor.getString(column_index));
			}

			if (bitmap == null)
				return;
			
			int index = 0;
			for(int i = 0; i < 9; i++)
			{
				if(mPhotoFile[i] == null)
				{
					index = i;
					break;
				}
			}
			
			mPhotoFile[index] = new File("/mnt/sdcard/" + "jb_tmp" + index + ".jpg");// 将要保存图片的路径
			mPhotoFile[index].deleteOnExit();
			try {
				BufferedOutputStream bos = new BufferedOutputStream(
						new FileOutputStream(mPhotoFile[index]));
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
				bos.flush();
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			mBitmap[index] = bitmap;
			findViewById(R.id.img0 + index).setVisibility(View.VISIBLE);
			((ImageView)findViewById(R.id.img0 + index)).setImageBitmap(mBitmap[index]);
		}
	}
}
